package com.web.dao.impl;

import com.web.dao.StudentDAO;
import com.web.entity.Student;
import com.web.util.JDBCUtils;

import java.util.List;

public class StudentDAOImpl implements StudentDAO {

    @Override
    public List<Student> findAllStudents() {
        return JDBCUtils.query(Student.class, "select * from student");
    }

    @Override
    public int deleteStudent(int id) {
        return JDBCUtils.update("delete from student where id = ?", id);
    }

    @Override
    public Student findStudentById(int id) {
        return JDBCUtils.query(Student.class, "select * from student where id = ?", id).get(0);
    }

    @Override
    public int insertStudent(Student student) {
        return JDBCUtils.update("insert into student(name, age, gender, cellphone, address) values (?, ?, ?, ?, ?)",
                student.getName(), student.getAge(), student.getGender(), student.getCellphone(), student.getAddress());
    }

    @Override
    public List<Student> searchStudentsByName(String keyword) {
        return JDBCUtils.query(Student.class, "select * from student where name like ?", "%" + keyword + "%");
    }

    @Override
    public int updateStudent(Student student) {
        return JDBCUtils.update("update student set name = ?, age = ?, gender = ?, cellphone = ?, address = ? where id = ?",
                student.getName(), student.getAge(), student.getGender(), student.getCellphone(), student.getAddress(), student.getId());
    }

}
